﻿using System;
using System.Windows;
using System.Windows.Input;
using Moonwish.Core;
using Moonwish.Core.Command;
using Moonwish.Foundation.Interface;
using Moonwish.Foundation.ValueObjects;
using Moonwish.Foundation.Views.ValueObjects;

namespace Moonwish.Foundation.ViewModels.ValueObjects
{
    /// <summary>
    /// 值对象ViewModel,write by zhlin 2010-03-13.
    /// </summary>
    public class ValueObjectViewModel : ViewModelBase, IFoundationCommands
    {
        private ValueObjectCollection _data;

        public ValueObjectViewModel()
        {
            if (!IsInDesignMode)
            {
                PublishCommand = new RelayCommand<ValueObject>(Publish);
                DisableCommand = new RelayCommand<ValueObject>(Disable);
            }
        }
        /// <summary>
        /// 数据源。
        /// </summary>
        public ValueObjectCollection Data
        {
            get { return _data; }
            set { 
                _data = value;

                RaisePropertyChanged(() => Data);

                if (value.Count > 0)
                {
                    SelectedItem = value[0];
                }
            }
        }

        private string _classId;

        /// <summary>
        /// 所属分类ID。
        /// </summary>
        public string ClassId
        {
            get { return _classId; }
            set { _classId = value; 
                RaisePropertyChanged(()=> CanAdd);
            }
        }

        private ValueObject _selectedItem;
        /// <summary>
        /// 当前选中数据行。
        /// </summary>
        public ValueObject SelectedItem { 
            get { return _selectedItem; }
            set
            {
                if (_selectedItem != value)
                {
                    _selectedItem = value;

                    RaisePropertyChanged("SelectedItem");

                    UpdateButtonStatus();
                }
            }
        }

        protected override void Add()
        {
            var newItem = new ValueObject(ClassId);
            newItem.ApplyButtonVisibility = Visibility.Visible;
            var dialog = new ValueObjectDialog(newItem);
            dialog.Title = "新增";
            dialog.Show();
            dialog.ApplyOk += (s, e) =>
            {
                Data.AddCompleted += (s1, e1) =>
                {
                    SelectedItem = newItem;
                    IsBusy = false;
                    newItem = new ValueObject(ClassId);
                    newItem.ApplyButtonVisibility = Visibility.Visible;
                    dialog.DataContext = newItem;
                    UpdateButtonStatus();
                };
                Data.Add(newItem);
                IsBusy = true;
            };
            dialog.Closed += (s, e) =>
            {
                if (dialog.DialogResult.HasValue && dialog.DialogResult.Value)
                {
                    Data.AddCompleted += (s1, e1) =>
                    {
                        IsBusy = false;
                        SelectedItem = newItem;
                        UpdateButtonStatus();
                    };
                    Data.Add(newItem);
                    IsBusy = true;
                }
            };
        }

        protected override void Edit(object selectedItem)
        {
            var originalItem = (ValueObject)selectedItem;
            var editItem = originalItem.Clone();
            editItem.ApplyButtonVisibility = Visibility.Collapsed;
            Edit(editItem, dialog =>
            {
                if (dialog.HasValue && dialog.Value)
                {
                    originalItem.Override(editItem);
                    originalItem.SaveChangedCompleted += (s, e) =>
                    {
                        IsBusy = false;
                        UpdateButtonStatus();
                    };
                    originalItem.SaveChangedAsync();
                    IsBusy = true;
                }
            });
        }

        protected override void Edit(object selectedItem, Action<bool?> finishedCallback)
        {
            var editItem = (ValueObject)selectedItem;
            var dialog = new ValueObjectDialog(editItem);
            dialog.Title = "编辑";
            dialog.Show();
            dialog.Closed += (s, e) =>
            {
                if (dialog.DialogResult.HasValue && dialog.DialogResult.Value)
                {
                    finishedCallback(dialog.DialogResult);
                }
            };
        }

        protected override void Delete(object selectedItem)
        {
            var dataItem = (ValueObject)selectedItem;
            if (MessageBox.Show(
                string.Format("确定要删除“{0}”吗？", dataItem.Name), "删除提示", MessageBoxButton.OKCancel)
                != MessageBoxResult.OK)
            {
                return;
            }
            Data.RemoveCompleted += (s, e) =>
            {
                IsBusy = false;
                UpdateButtonStatus();
            };
            Data.Remove(dataItem);
            IsBusy = true;
        }

        public ICommand PublishCommand { get; set; }

        public void Publish(object selectedItem)
        {
            var dataItem = (ValueObject)selectedItem;
            if (MessageBox.Show(
                 string.Format("确定要发布“{0}”吗？", dataItem.Name), "发布提示", MessageBoxButton.OKCancel)
                 != MessageBoxResult.OK)
            {
                return;
            }
            dataItem.PublishCompleted += (s, e) =>
            {
                IsBusy = false;
                UpdateButtonStatus();
            };
            dataItem.PublishAsync();
            IsBusy = true;
        }

        public ICommand DisableCommand { get; set; }

        public void Disable(object selectedItem)
        {
            var dataItem = (ValueObject)selectedItem;
            if (MessageBox.Show(
                 string.Format("确定要作废“{0}”吗？", dataItem.Name), "作废提示", MessageBoxButton.OKCancel)
                 != MessageBoxResult.OK)
            {
                return;
            }
            dataItem.DisableCompleted += (s, e) =>
            {
                IsBusy = false;
                UpdateButtonStatus();
            };
            dataItem.DisableAsync();
            IsBusy = true;
        }

        public void UpdateButtonStatus()
        {
            RaisePropertyChanged(() => CanEdit);
            RaisePropertyChanged(() => CanPublish);
            RaisePropertyChanged(() => CanDisable);
            RaisePropertyChanged(() => CanDelete);
        }

        public bool CanAdd
        {
            get { return !string.IsNullOrEmpty(ClassId); }
        }

        public bool CanEdit
        {
            get { return SelectedItem != null; }
        }

        public bool CanPublish
        {
            get
            {
                return SelectedItem != null && SelectedItem.DataStatus == DataStatus.New;
            }
        }

        public bool CanDisable
        {
            get
            {
                return SelectedItem != null && SelectedItem.DataStatus == DataStatus.Released;
            }
        }

        public bool CanDelete
        {
            get { return SelectedItem != null && SelectedItem.DataStatus == DataStatus.New; }
        }
    }
}
